package vn.com.vega.music.view.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.objects.Song;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SelectSongPlayAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Boolean> mListSelected;
	private SelectSongPlayListener mSelectListener;
	private List<Song> mListSong;
	public int numberSelect = 0;

	public SelectSongPlayAdapter(Context ctx, List<Song> list,
			SelectSongPlayListener listener) {
		mContext = ctx;
		mSelectListener = listener;
		mListSong = list;
		if (list == null || list.size() == 0) {
			return;
		}
		mInflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mListSelected = new ArrayList<Boolean>(mListSong.size());
		setSelectNone();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListSong.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListSong.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder hoder;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.select_song_play_item_list, null);
			hoder = new ViewHolder();
			hoder.songtitle = (TextView) convertView
					.findViewById(R.id.tv_song_title_select_play);
			hoder.songArtist = (TextView) convertView
					.findViewById(R.id.tv_song_atist);
			hoder.checkSongSelect = (ImageView) convertView
					.findViewById(R.id.cb_song_select_play);
			convertView.setTag(hoder);
		} else {
			hoder = (ViewHolder) convertView.getTag();
		}

		Song song = (Song) getItem(position);
		hoder.songtitle.setText(song.title);
		hoder.songArtist.setText(song.artist_name);
		final int index = position;
		hoder.checkSongSelect.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!hoder.checkSongSelect.isSelected()) {
					hoder.checkSongSelect.setSelected(true);
					numberSelect++;
					mListSelected.set(index, true);
					if (mSelectListener != null) {
						mSelectListener.changeNumberSongSelect();
					}
				} else {
					hoder.checkSongSelect.setSelected(false);
					numberSelect--;
					mListSelected.set(index, false);
					if (mSelectListener != null) {
						mSelectListener.changeNumberSongSelect();
					}
				}
			}
		});
		if (mListSelected.get(position)) {
			hoder.checkSongSelect.setSelected(true);
		} else {
			hoder.checkSongSelect.setSelected(false);
		}
		return convertView;
	}

	static class ViewHolder {
		TextView songtitle;
		TextView songArtist;
		ImageView checkSongSelect;
	}

	public void setSelectAll() {
		if (mListSelected == null) {
			mListSelected = new ArrayList<Boolean>(mListSong.size());
		}
		numberSelect = getCount();
		mListSelected.clear();
		for (int i = 0; i < mListSong.size(); i++) {
			mListSelected.add(i, true);
		}
		notifyDataSetChanged();
		if (mSelectListener != null) {
			mSelectListener.changeNumberSongSelect();
		}
	}

	public void setSelectNone() {
		if (mListSelected == null) {
			mListSelected = new ArrayList<Boolean>(mListSong.size());
		}
		numberSelect = 0;
		mListSelected.clear();
		for (int i = 0; i < mListSong.size(); i++) {
			mListSelected.add(i, false);
		}
		notifyDataSetChanged();
		if (mSelectListener != null) {
			mSelectListener.changeNumberSongSelect();
		}
	}

	public void changeStateForPosition(int position) {
		if (position < 0 || position >= getCount()) {
			return;
		}
		if (mListSelected.get(position)) {
			mListSelected.set(position, false);
			numberSelect--;
			if (mSelectListener != null) {
				mSelectListener.changeNumberSongSelect();
			}
		} else {
			mListSelected.set(position, true);
			numberSelect++;
			if (mSelectListener != null) {
				mSelectListener.changeNumberSongSelect();
			}
		}

		notifyDataSetChanged();
	}

	public List<Song> getListSelected() {
		List<Song> result = new ArrayList<Song>();
		if (mListSelected != null) {
			for (int i = 0; i < mListSelected.size(); i++) {
				if (mListSelected.get(i)) {
					result.add(mListSong.get(i));
				}
			}
		}
		return result;
	}

	public interface SelectSongPlayListener {
		// public void haveSongSelect();
		//
		// public void noneSongSelect();
		public void changeNumberSongSelect();
	}

}
